package com.whq.knowverywell.activity;

import java.util.ArrayList;
import java.util.List;

import android.R.color;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.SubMenu;
import com.whq.knowverywell.R;
import com.whq.knowverywell.adapter.HomeAdapter;
import com.whq.knowverywell.business.model.HomeItem;
import com.whq.knowverywell.task.ChkAppUpdateTask;
import com.whq.knowverywell.util.DisplayUtil;
import com.whq.knowverywell.util.LogUtil;
import com.whq.knowverywell.util.constant.ModelConstant;
import com.whq.knowverywell.util.constant.ParamConstant;

/**
 * 登录后首页
 * @author wanghuaiqiang
 *
 */
public class HomeActivity extends BaseLoginActivity implements OnItemClickListener {
	private  static final String TAG = HomeActivity.class.getSimpleName();
	
	private GridView mGridView;
	private HomeAdapter mAdapter;
	private List<HomeItem> mItemsList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		LogUtil.d(TAG, "onCreate start ...");
		setTheme(R.style.Theme_Sherlock_Light);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		
		initData();
		
		mGridView = (GridView) findViewById(R.id.home_gridview);
		mAdapter = new HomeAdapter(this, mItemsList);
		mGridView.setAdapter(mAdapter);
		mGridView.setCacheColorHint(0);
		mGridView.setBackgroundColor(color.transparent);
		mGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		mGridView.setOnItemClickListener(this);
		mHiddenManagerImageView = (ImageView) findViewById(R.id.manager_imageview);
		
		getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.top_menu_bg));
		getSupportActionBar().setIcon(R.drawable.home_btn_n);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		
	}
	
	@Override
	protected void onResume() {
		LogUtil.d(TAG, "onResume start ...");
		super.onResume();
		
//		// 检测是否收到升级的参数
//		Intent intent = getIntent();
//		if (intent != null) {
//			NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//			nm.cancel(CfgConstant.UPDATE_NOTIFY_ID);
//			
//			String appVersionName = intent.getStringExtra(ParamUtil.BUNDLE_KEY_APP_VERSION_NAME);
//			String appApkName = intent.getStringExtra(ParamUtil.BUNDLE_KEY_APP_APK_NAME);
//			String appIntro = intent.getStringExtra(ParamUtil.BUNDLE_KEY_APP_INTRO);
//			if ( StringUtil.isNotBlank(appVersionName)
//					&& StringUtil.isNotBlank(appApkName)
//					) {
//				AppUpdateUtil.doNewVersionUpdate(HomeActivity.this, appVersionName, appApkName, appIntro);
//				return ;
//			}
//		}
		
		// 定期检测版本更新
		new ChkAppUpdateTask(this, ChkAppUpdateTask.CHK_UPDATE_TYPE_AUTO, false).execute();
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View convertView, int position, long id) {
		LogUtil.d(TAG, "onItemClick view id[" + id + "] position[" + position + "]");
		HomeItem item = mAdapter.getItem(position);
		LogUtil.i(TAG, "clicked item is " + item.toString());
		
		if (position == 3) {
			// forward to product search activity
			Intent intent = new Intent();
			intent.setClass(this, ProductSearchActivity.class);
			intent.putExtra(ParamConstant.BUNDLE_KEY_ITEM_ID, item.getItemId());
			startActivity(intent);
		} else {
			// forward to frame activity
			Intent intent = new Intent();
			intent.setClass(this, ContentFrameActivity.class);
			intent.putExtra(ParamConstant.BUNDLE_KEY_ITEM_ID, item.getItemId());
			startActivity(intent);
		}
	}

	@Override
	public void onClick(View v) {
		LogUtil.d(TAG, "onClick view id[" + v.getId() + "]");
		super.onClick(v);
		
		switch (v.getId()) {
//		case R.id.more:
//			TODO
//			break;

		default:
			break;
		}
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        SubMenu sub = menu.addSubMenu("管理");
        sub.setIcon(R.drawable.btn_set);
        sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mManagerPopupWindow != null && !mManagerPopupWindow.isShowing()) {
            mManagerPopupWindow.showAsDropDown(mHiddenManagerImageView, 0, DisplayUtil.px2dip(-60, mDisplayMetrics.density));
        }
        return true;
    }
    
    @Override
    protected void getParameters() {
    	super.getParameters();
    	Intent intent = getIntent();
		if (intent != null) {
			// TODO  这里需要帐户数组信息、零售店的ID
		}
    }
	
	// ------------- customize method -------------
	private void initData() {
		mItemsList = new ArrayList<HomeItem>();
		mItemsList.add(new HomeItem(ModelConstant.BUSINESS_TYPE_SUMMARY, "汇总统计", R.drawable.home_item_summary));
		mItemsList.add(new HomeItem(ModelConstant.BUSINESS_TYPE_DOMAIN, "地域分析", R.drawable.home_item_domain));
		mItemsList.add(new HomeItem(ModelConstant.BUSINESS_TYPE_SORT, "品类分析", R.drawable.home_item_sort));
		mItemsList.add(new HomeItem(ModelConstant.BUSINESS_TYPE_GOODS, "货品查询", R.drawable.home_item_goods));
		mItemsList.add(new HomeItem(ModelConstant.BUSINESS_TYPE_HOTSELL, "热销查询", R.drawable.home_item_hotsell));
	}

}